<template>

  <el-dialog title="解冻操作" :visible.sync="dialogVisible" :close-on-click-modal="false">

    <el-form ref="main-form" :model="form" :rules="rules" label-width="80px" :inline="true" >
    	<el-form-item label="订单号" size="mini" prop="tradeNo">
        <el-input placeholder="" disabled v-model="form.tradeNo" @keyup.enter.native="submit" :readonly="tradeNo == null" clearable></el-input>
      </el-form-item>

      <el-form-item label="原因" size="mini" prop="transReason">
        <!-- <el-input placeholder="" v-model="form.transReason" @keyup.enter.native="submit" clearable></el-input> -->
        <el-input placeholder="" v-model="form.transReason" @keyup.enter.native="submit" :readonly="tradeNo == null" clearable></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close" size="small">取 消</el-button>
      <el-button type="primary" @click="submit" size="small" :loading="loading">提 交</el-button>
    </div>

	</el-dialog>

</template>

<script>

import ApiMain from '@/api/trade.thaw';

export default {
  mixins: [
  ],
  props: [
  ],
  data () {
    return {

      dialogVisible: false,
      loading: false,

      form: {
        tradeNo: '',
        transReason : '',
        transReason: '',
        remark: '',
      },

      rules: {
        tradeNo: [
          { required: true, message: '请输入单号', trigger: 'blur' },
        ],
        transReason: [
          { required: true, message: '请输入原因', trigger: 'blur' },
        ],
      },

      tradeNo  : null,
      transReasonIdOptions: [],
    }
  },
  mounted () {
  },

  methods: {

    show (tradeNo = null) {

      this.tradeNo = tradeNo;
      this.reset();
      this.form.tradeNo = tradeNo;
      this.dialogVisible = true;
      
    },

    close () {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset () {
      this.form = {
        tradeNo: '',
        transAmount: '',
        transReason : '',
        remark: ''
      };
    },

    verifyForm () {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit () {

      if (! this.verifyForm()) return false;

      this.loading = true;

      try {
        if (this.id) {
          await this.submitSet();
        }
        else {
          await this.submitAdd();
        }
      } finally {
        this.loading = false;
      }
    },

    async submitSet () {

      this.form.id = this.id;
      await ApiMain.set(this.form);
      this.$message({message: '修改成功', type: 'success'});
      this.close();

    },

    async submitAdd () {

      let form = Object.assign({}, this.form);
      form.transAmount = Number((form.transAmount * 100).toFixed(0))
      await ApiMain.add(form);
      this.$message({message: '添加成功', type: 'success'});
      this.close();
    },
  }
}
</script>